<?PHP
include "../../../mainfile.php";

//upadte configuration
if($_POST['action'] == 'configuration'){
	$xoopsDB->query("UPDATE " . $xoopsDB->prefix('program_schedule_configuration') . " SET start_time = '".$_POST['start_time']."', view = '".$_POST['view']."', DailyViewCurrent = '".$_POST['current']."', announcements = '".$_POST['announcements']."', Show_NP_Image = '".$_POST['Show_NP_Image']."', Show_NP_Description = '".$_POST['Show_NP_Description']."', letters = '".strtoupper($_POST['letters'])."'");
	unset($_SESSION['view']);
redirect_header('index.php?op=config&currentID=0', 1, 'Your station info has succesfully been updated!');
}


//ADD NEW PROGRAM CATEGORY
if($_POST['action'] == 'add_category'){
	$color = ereg_replace("[^A-Za-z0-9]", "", $_POST['color']);
	$xoopsDB->query("INSERT INTO " . $xoopsDB->prefix('program_schedule_categories') . " values ('0','".mysql_real_escape_string($_POST['description'])."','$color')");
	redirect_header('index.php?op=categories&currentID=1', 1, "You have succesfully added the following category: ".$_POST['description'].".");
}


//EDIT PROGRAM CATEGORY
if($_POST['action'] == 'edit_category'){
	$color = ereg_replace("[^A-Za-z0-9]", "", $_POST['color']);
	$xoopsDB->query("UPDATE " . $xoopsDB->prefix('program_schedule_categories') . " SET description = '".mysql_real_escape_string($_POST['description'])."', color = '$color' WHERE ID = '".$_POST['ID']."'");
	redirect_header('index.php?op=categories&currentID=1', 1, "You have succesfully updated the following category: ".$_POST['description'].".");
}


//DELETE PROGRAM CATEGORY
if($_POST['action'] == 'delete_category'){
	$xoopsDB->query("DELETE FROM ".$xoopsDB->prefix("program_schedule_categories")." WHERE ID=".$_POST['ID']."");
	redirect_header('index.php?op=categories&currentID=1', 1, "You have succesfully deleted the following category: ".$_POST['name'].".");
}



//ADD NEW PROGRAM
if($_POST['action'] == 'add_program'){




	include_once '../../../class/uploader.php';
	$allowed_mimetypes = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png');
	$maxfilesize = '15000';
	$maxfilewidth = '150';
	$maxfileheight = '150';
	$uploader = new XoopsMediaUploader(XOOPS_ROOT_PATH . "/" . "modules/program_schedule/images/uploads", $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight);
		if ($uploader->fetchMedia($_POST["xoops_upload_file"][0])) {
			if (!$uploader->upload()) {
					$error = $uploader->getErrors();
			} else {
				// we have a file
			$fileName = $uploader->getSavedFileName();
			}
		} else {
			$error = $uploader->getErrors();
		} 

	//+++++++++++++++++++++++++++++++++++++++++
	//INSERT STUFF INTO DB ++++++++++++++++++++
	//+++++++++++++++++++++++++++++++++++++++++
	
	$formDays =array();
	
	
	
	$weekDays = array('mon','tue','wed','thu','fri','sat','sun');
	for ($i = 0; $i < 7; $i++) {
	
		if($_POST[$weekDays[$i]] == 'true'){
		$weekDays[$i] = 'true';
		}else{
			$weekDays[$i] = 'false';
		}
		

	}
	
	if($_POST['link'] == 'http://'){
		$link = "";
	}else{
		$link = $_POST['link'];
	}
	
	
	
	//create separate entries for non consecutive days
	$consecutiveDays = array();
	$TempWeekDays = array('mon','tue','wed','thu','fri','sat','sun');
	$tempBuild = array();
	for ($i = 0; $i < (count($weekDays)+1); $i++) {
		if($weekDays[$i] == 'true'){
			$consecutiveDays[] = $TempWeekDays[$i];
		}else{
			if(count($consecutiveDays) > '0'){
				for ($l = 0; $l < count($TempWeekDays); $l++) {
					if(in_array($TempWeekDays[$l],$consecutiveDays)){
						$tempBuild[$l] = 'true';
					}else{
						$tempBuild[$l] = 'false';
					}
				}//end inner loop
				
				//put stuff into DB
				if($fileName){
					$_SESSION['message'] = "You have succesfully added the following program: ".$_POST['title'].".";
					$xoopsDB->query("INSERT INTO " . $xoopsDB->prefix('program_schedule_programs') . " values ('0','".mysql_real_escape_string($_POST['title'])."','".$_POST['category']."','".mysql_real_escape_string($_POST['description'])."','".$_POST['start_time']."','".$_POST['end_time']."','".$link."','".$fileName."','".$_POST['DJ']."','".$tempBuild[0]."','".$tempBuild[1]."','".$tempBuild[2]."','".$tempBuild[3]."','".$tempBuild[4]."','".$tempBuild[5]."','".$tempBuild[6]."')");
				}elseif(strstr($error, 'Filename Is Empty') == FALSE){
					$fileName = "";
					$_SESSION['message'] = "You have succesfully added the following program: ".$_POST['title'].". <BR>" . $error;		
					$xoopsDB->query("INSERT INTO " . $xoopsDB->prefix('program_schedule_programs') . " values ('0','".mysql_real_escape_string($_POST['title'])."','".$_POST['category']."','".mysql_real_escape_string($_POST['description'])."','".$_POST['start_time']."','".$_POST['end_time']."','".$link."','".$fileName."','".$_POST['DJ']."','".$tempBuild[0]."','".$tempBuild[1]."','".$tempBuild[2]."','".$tempBuild[3]."','".$tempBuild[4]."','".$tempBuild[5]."','".$tempBuild[6]."')");
				}else{
					$_SESSION['message'] = "You have succesfully added the following program: ".$_POST['title'].".";
					$fileName = "";
					$xoopsDB->query("INSERT INTO " . $xoopsDB->prefix('program_schedule_programs') . " values ('0','".mysql_real_escape_string($_POST['title'])."','".$_POST['category']."','".mysql_real_escape_string($_POST['description'])."','".$_POST['start_time']."','".$_POST['end_time']."','".$link."','".$fileName."','".$_POST['DJ']."','".$tempBuild[0]."','".$tempBuild[1]."','".$tempBuild[2]."','".$tempBuild[3]."','".$tempBuild[4]."','".$tempBuild[5]."','".$tempBuild[6]."')");
				} 
						
				
			}//end if
			
		$consecutiveDays = array();
		}//end else
	}//end main loop
	
	
	//print_r($weekDays);
	
	
	
	
	redirect_header('index.php?op=addProgram&currentID=2', 1, $_SESSION['message']);

	

}



//EDIT PROGRAM 
if($_POST['action'] == 'edit_program'){

	include_once '../../../class/uploader.php';
	$allowed_mimetypes = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png');
	$maxfilesize = '15000';
	$maxfilewidth = '150';
	$maxfileheight = '150';
	$uploader = new XoopsMediaUploader(XOOPS_ROOT_PATH . "/" . "modules/program_schedule/images/uploads", $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight);
		if ($uploader->fetchMedia($_POST["xoops_upload_file"][0])) {
			if (!$uploader->upload()) {
					$error = $uploader->getErrors();
			} else {
				// we have a file
			$fileName = $uploader->getSavedFileName();
			}
		} else {
			$error = $uploader->getErrors();
		} 




		$weekDays = array('mon','tue','wed','thu','fri','sat','sun');
	for ($i = 0; $i < 7; $i++) {
		if (in_array ($weekDays[$i], $_POST['days'])){
			$weekDays[$i] = 'true';
		}else{
			$weekDays[$i] = 'false';
		}
	}
	
		if($_POST['link'] == 'http://'){
		$link = "";
	}else{
		$link = $_POST['link'];
	}
	
	
	if($fileName){
		$xoopsDB->query("UPDATE " . $xoopsDB->prefix('program_schedule_programs') . " SET title = '".mysql_real_escape_string($_POST['title'])."', category = '".$_POST['category']."', description = '".mysql_real_escape_string($_POST['description'])."', start_time = '".$_POST['start_time']."', end_time = '".$_POST['end_time']."', link = '".$link."', image = '$fileName', DJ = '".$_POST['DJ']."', mon = '".$weekDays[0]."', tue = '".$weekDays[1]."', wed = '".$weekDays[2]."', thu = '".$weekDays[3]."', fri = '".$weekDays[4]."', sat = '".$weekDays[5]."',sun = '".$weekDays[6]."' WHERE ID = '".$_POST['update-ID']."'");
		$_SESSION['message'] = "You have succesfully updated the following Program: ".$_POST['title'].".";
	}elseif(strstr($error, 'Filename Is Empty') == FALSE){
		$xoopsDB->query("UPDATE " . $xoopsDB->prefix('program_schedule_programs') . " SET title = '".mysql_real_escape_string($_POST['title'])."', category = '".$_POST['category']."', description = '".mysql_real_escape_string($_POST['description'])."', start_time = '".$_POST['start_time']."', end_time = '".$_POST['end_time']."', link = '".$link."', DJ = '".$_POST['DJ']."', mon = '".$weekDays[0]."', tue = '".$weekDays[1]."', wed = '".$weekDays[2]."', thu = '".$weekDays[3]."', fri = '".$weekDays[4]."', sat = '".$weekDays[5]."',sun = '".$weekDays[6]."' WHERE ID = '".$_POST['update-ID']."'");
		$_SESSION['message'] = "You have succesfully updated the following Program: ".$_POST['title'].". <BR>".$error;
	}else{
		$xoopsDB->query("UPDATE " . $xoopsDB->prefix('program_schedule_programs') . " SET title = '".mysql_real_escape_string($_POST['title'])."', category = '".$_POST['category']."', description = '".mysql_real_escape_string($_POST['description'])."', start_time = '".$_POST['start_time']."', end_time = '".$_POST['end_time']."', link = '".$link."', DJ = '".$_POST['DJ']."', mon = '".$weekDays[0]."', tue = '".$weekDays[1]."', wed = '".$weekDays[2]."', thu = '".$weekDays[3]."', fri = '".$weekDays[4]."', sat = '".$weekDays[5]."',sun = '".$weekDays[6]."' WHERE ID = '".$_POST['update-ID']."'");
		$_SESSION['message'] = "You have succesfully updated the following Program: ".$_POST['title'].".";
	}
	redirect_header('index.php?op=managePrograms&currentID=3', 1, $_SESSION['message']);
}

//DELETE PROGRAM
if($_POST['action'] == 'delete_program'){ 
	$xoopsDB->query("DELETE FROM ".$xoopsDB->prefix("program_schedule_programs")." WHERE ID=".$_POST['ID']."");
	redirect_header('index.php?op=managePrograms&currentID=3', 1, "You have succesfully deleted the selected program: ".$_POST['name']." ");
}

?>